package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import kong.unirest.HttpResponse;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;


public class AcademicPerformanceController {
    @FXML
    private Label completedCredit;
    @FXML
    private Label gpa;
    @FXML
    private Label coursesNumber;
    @FXML
    private Label classTime;
    @FXML
    private ScrollPane Box;

    public void set(long studentId) throws IOException {
        JSONObject res = HttpUtil.get(Globalvar.baseUrl + "/api/students/" + studentId + "/scores")
                        .asJson().getBody().getObject();
        JSONObject dataObject = res.getJSONObject("data");
        JSONArray scoresArray = res.getJSONArray("scores");
        completedCredit.setText(dataObject.getString("credit"));
        gpa.setText(dataObject.getString("gpa"));
        for(int i=0;i<scoresArray.length();i++){
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/academic-performance-item.fxml"));
            Parent root = fxmlLoader.load();
            AcademicPerformanceItemController APIC = fxmlLoader.getController();
            APIC.setItem(((JSONObject)scoresArray.get(i)).getJSONObject("course").getString("name"),((JSONObject)scoresArray.get(i)).getJSONObject("course").getString("teacherName"),((JSONObject)scoresArray.get(i)).getJSONObject("course").getString("credit"),((JSONObject)scoresArray.get(i)).getJSONObject("course").getString("termName"),((JSONObject)scoresArray.get(i)).getJSONObject("course").getBoolean("ended"),((JSONObject)scoresArray.get(i)).getString("score"));
            Box.setContent(root);
        }
    }

}
